﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using ImagesNameConvert.Helper;

namespace ImagesNameConvert
{
	public partial class ReadExcelPic : Form
	{
		//允许的图片格式，读取时只读取这些格式的图片文件
		private readonly string[] AllowFileType = new string[] { ".jpg", ".png", ".jpeg", ".gif", ".bmp" };

		//每个目录要处理的最大图片数量
		private readonly int MaxCount = 5;

		//要处理的目录地址
		private string FolderPath;

		private delegate void SetTextBox2Handler(string FolderName);
		public ReadExcelPic()
		{
			InitializeComponent();
		}

		private void button1_Click(object sender, EventArgs e)
		{
			if (this.folderBrowserDialog1.ShowDialog() == DialogResult.OK)
			{
				if (this.folderBrowserDialog1.SelectedPath.Trim() != "")
					textBox1.Text = this.folderBrowserDialog1.SelectedPath.Trim();
			}
		}

		private void button2_Click(object sender, EventArgs e)
		{
			FolderPath = textBox1.Text;
			if (string.IsNullOrEmpty(FolderPath))
			{
				MessageBox.Show("请选择图片目录");
				return;
			}
			Thread t = new Thread(new ThreadStart(Run));
			t.Start();
		}

		private void SetTextBox2(string Result)
		{
			if (textBox2.InvokeRequired == true)
			{
				SetTextBox2Handler set = new SetTextBox2Handler(SetTextBox2);
				textBox2.Invoke(set, new object[] { Result });
			}
			else
			{
				textBox2.Text += Result + "\r\n";
			}

		}

		private void Run()
		{
			SetTextBox2Handler handler = new SetTextBox2Handler(SetTextBox2);

			DataTable table = new DataTable();
			table.Columns.Add(new DataColumn() { ColumnName = "文件夹名称", DataType = typeof(string) });
			table.Columns.Add(new DataColumn() { ColumnName = "图片1", DataType = typeof(string) });
			table.Columns.Add(new DataColumn() { ColumnName = "图片2", DataType = typeof(string) });
			table.Columns.Add(new DataColumn() { ColumnName = "图片3", DataType = typeof(string) });
			table.Columns.Add(new DataColumn() { ColumnName = "图片4", DataType = typeof(string) });
			table.Columns.Add(new DataColumn() { ColumnName = "图片5", DataType = typeof(string) });
			table.Columns.Add(new DataColumn() { ColumnName = "图片6", DataType = typeof(string) });
			table.Columns.Add(new DataColumn() { ColumnName = "图片7", DataType = typeof(string) });
			table.Columns.Add(new DataColumn() { ColumnName = "图片8", DataType = typeof(string) });
			table.Columns.Add(new DataColumn() { ColumnName = "图片9", DataType = typeof(string) });
			table.Columns.Add(new DataColumn() { ColumnName = "图片10", DataType = typeof(string) }); 

			#region 读取下一层目录，只读取一层
			var List = Directory.GetDirectories(FolderPath);
			foreach (var item in List)
			{
				//读取目录下图片文件
				var FileList = Directory.GetFiles(item).Where(m => AllowFileType.Contains(Path.GetExtension(m).ToLower())).ToList();
				if (FileList.Count == 0)
					continue;

				//最多只处理设定的图片数量，超出部分不处理
				int CurrentCount = FileList.Count;
				if (CurrentCount > MaxCount)
					CurrentCount = MaxCount;

				//开始处理图片
				DataRow row = table.NewRow();
				string FolderName = new DirectoryInfo(item).Name; 
				row["文件夹名称"] = FolderName;
				for (int i = 0; i < CurrentCount; i++)
				{
					row["图片" + (i + 1)] = ConfigurationManager.AppSettings["PreDomain"].TrimEnd('/') + "/" + FolderName + "/" + Path.GetFileName(FileList[i]);
				}
				table.Rows.Add(row);

				//写入textbox文本框
				handler("目录   " + item + "   处理成功");
			}

			handler("正在写入Excel");

			var Excel = new ExcelHelper("处理结果.xlsx");
			Excel.DataTableToExcel(table, "sheet1", true);

			//处理完成
			handler("处理完成");
			#endregion
		}
	}
}
